#!perl -w
use strict;
use warnings;

######################################################################
# net-google-picasaweb project. Implementing the API detailed at:
# http://code.google.com/apis/picasaweb/reference.html
#
# Copyright (C) 2007 by Robert May
#
# This library is free software; you can redistribute it and/or modify
# it under the same terms as Perl itself.
######################################################################
# $Id$
######################################################################

package TestServer::Config::ClientLogin;

our @ISA = qw( TestServer::Config );

use URI::Escape qw(uri_unescape);

sub get_base {
	return '/ClientLogin';
}

my $good_content = <<'EOC';
SID=DQAAAGgA...7Zg8CTN
LSID=DQAAAGsA...lk8BBbG
Auth=DQAAAGgA...dk3fA5N
EOC

my $noauth_content = <<'EOC';
SID=DQAAAGgA...7Zg8CTN
LSID=DQAAAGsA...lk8BBbG
EOC

my $captcha_extra_content = <<'EOC';
Url=http://www.google.com/login/captcha
CaptchaToken=DQAAAGgA...dkI1LK9
CaptchaUrl=Captcha?ctoken=HiteT4b0Bk5Xg18_AcVoP6-yFkHPibe7O9EqxeiI7lUSN
EOC

my %responses = (
	'/ClientLogin/good'         => [ 200, "OK", [ Content_Type => 'text/plain' ], $good_content],
	'/ClientLogin/noauthtoken'  => [ 200, "OK", [ Content_Type => 'text/plain' ], $noauth_content ],
	'/ClientLogin/bad'          => [ 500, "Internal Server Error", [], "Internal Server Error"  ],
);

my @errors = qw( BadAuthentication NotVerified TermsNotAgreed
                 CaptchaRequired Unknown AccountDeleted AccountDisabled
                 ServiceDisabled ServiceUnavailable );

for my $error (@errors, 'NON_EXISTANT') {
    my $url = "/ClientLogin/error/$error";
    my $bad_content = "Error=$error";
    $bad_content .= "\n$captcha_extra_content" if $error eq 'CaptchaRequired';
    $responses{$url} = [ 403, "Access Forbidden", [ Content_Type => 'text/plain' ], $bad_content ];
}

sub get_response {
	my ($self, $path, $request) = @_;

	my $response;

	if (exists $responses{$path}) {
		$response = HTTP::Response->new(@{$responses{$path}});
	}

    unless ($response) {
        if ( $path =~ m|^/ClientLogin/user| ) {
            # Extract user from request
            my $c = $request->content;

            my ($user) = $c =~ m/Email=(.+?)(\&|\s+|$)/;
            $user = uri_unescape($user);
            $response = HTTP::Response->new( 500, "Internal Server Error", [], "User=$user=" );
        }
        elsif ( $path =~ m|^/ClientLogin/error/garbage| ) {
            $response = HTTP::Response->new( 403, "Access Forbiddeg", [],
                "Garbage with no error parameter" );
        }
    }

	return $response;
}

1; # MUST RETURN TRUE
